package com.example.springboottools.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Arrays;
import java.util.Objects;

/**
 * @Author:cyf
 * @CreateTime:2023-12-14
 */

public class OssClient {
    private String uploadUrl;

    private String downloadUrl;

    private String token;



    public static void main(String[] args) throws IOException {
        //uploadFontTest();
      //  uploadPodHtmlTest();
      //  uploadPodStampPng();
        uploadPodHtml();
    }

    private static void uploadPodHtml() throws IOException {
       String  uploadUrlS = "https://rgw.staplescn.com/api/stbchina-sales";
      // String  uploadUrlS = "http://rgw.staplescn.com/api/stbchina-sales";
        //String  uploadUrlS = "http://rgw.uat.stbchina.cn/api/staticadv";
        String  downloadUrlS = "http://salesoss.stbchina.cn/";
        String  tokenS = "4de60056aeff5de46a318493e79bf024a6dbe657";
      //   String  tokenS = "52acb64987804bb5a74666f9ad6b32bbee65da50";
        OssClient ossClient = new OssClient(uploadUrlS, downloadUrlS, tokenS);
        String filePath = "/prod/template/pod_html/";
        // String filePath = "/po_pdf/test/POD-H2.html";
        String fileUrl = "D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\新的模版修改\\标准模板\\V1\\S1X-V1.html";
        File file = new File(fileUrl);
        byte[] bytes = fileToByteArray(file);
        String s = ossClient.uploadFileByByteReturnUrl(filePath, bytes, "S1X-V1.html");
        System.out.println("文件访问地址：" + s);
    }
    private static void uploadPodHtmlTest() throws IOException {
       String  uploadUrlS = "https://rgw.staplescn.com/api/stbchina-sales";
      // String  uploadUrlS = "http://rgw.staplescn.com/api/stbchina-sales";
        //String  uploadUrlS = "http://rgw.uat.stbchina.cn/api/staticadv";
        String  downloadUrlS = "http://salesoss.stbchina.cn/";
        String  tokenS = "4de60056aeff5de46a318493e79bf024a6dbe657";
      //   String  tokenS = "52acb64987804bb5a74666f9ad6b32bbee65da50";
        OssClient ossClient = new OssClient(uploadUrlS, downloadUrlS, tokenS);
        String filePath = "/test/template/pod_html/";
        // String filePath = "/po_pdf/test/POD-H2.html";
        String fileUrl = "D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\新的模版修改\\标准模板\\V1\\S0-V1.html";
        File file = new File(fileUrl);
        byte[] bytes = fileToByteArray(file);
        String s = ossClient.uploadFileByByteReturnUrl(filePath, bytes, "S0-V1.html");
        System.out.println("文件访问地址：" + s);
    }
    private static void uploadFontTest() throws IOException {
       String  uploadUrlS = "https://rgw.staplescn.com/api/stbchina-sales";
      // String  uploadUrlS = "http://rgw.staplescn.com/api/stbchina-sales";
        //String  uploadUrlS = "http://rgw.uat.stbchina.cn/api/staticadv";
        String  downloadUrlS = "http://salesoss.stbchina.cn/";
        String  tokenS = "4de60056aeff5de46a318493e79bf024a6dbe657";
      //   String  tokenS = "52acb64987804bb5a74666f9ad6b32bbee65da50";
        OssClient ossClient = new OssClient(uploadUrlS, downloadUrlS, tokenS);
        String filePath = "/test/template/font/";
        // String filePath = "/po_pdf/test/POD-H2.html";
        String fileUrl = "D:\\staples\\test\\pod\\AlibabaPuHuiTi-3-45-Light.ttf";
        File file = new File(fileUrl);
        byte[] bytes = fileToByteArray(file);
        String s = ossClient.uploadFileByByteReturnUrl(filePath, bytes, "AlibabaPuHuiTi-3-45-Light.ttf");
        System.out.println("文件访问地址：" + s);
    }
    private static void uploadPodStampPng() throws IOException {
        String  uploadUrlS = "https://rgw.staplescn.com/api/stbchina-sales";
        //String  uploadUrlS = "http://rgw.uat.stbchina.cn/api/staticadv";
        String  downloadUrlS = "http://salesoss.stbchina.cn/";
        String  tokenS = "4de60056aeff5de46a318493e79bf024a6dbe657";
        // String  tokenS = "52acb64987804bb5a74666f9ad6b32bbee65da50";
        OssClient ossClient = new OssClient(uploadUrlS, downloadUrlS, tokenS);
        String filePath = "/prod/template/pod_stamp/";
        // String filePath = "/po_pdf/test/POD-H2.html";
        String fileUrl = "D:\\staples\\日常记录\\工作记录\\2025\\03\\POD迁移\\新的模版修改\\delivery_stamp.png";
        File file = new File(fileUrl);
        byte[] bytes = fileToByteArray(file);
        String s = ossClient.uploadFileByByteReturnUrl(filePath, bytes, "delivery_stamp.png");
        System.out.println("文件访问地址：" + s);
    }


    public static byte[] fileToByteArray(File file) throws IOException {
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
        }
        return byteArrayOutputStream.toByteArray();
    }

    public OssClient(String uploadUrl, String downloadUrl, String token) {
        this.uploadUrl = uploadUrl;
        this.downloadUrl = downloadUrl;
        this.token = token;
    }

    public HttpResponse uploadFile(String filePath, MultipartFile file, String fileName) throws IOException {
        Objects.requireNonNull(filePath);
        Objects.requireNonNull(file);
        Objects.requireNonNull(fileName);
        String bucketUrl = uploadUrl;
        HttpRequest request = HttpUtil.createPost(bucketUrl)
                .header("Authorization", "Token " + token)
                .form("filename", file.getBytes(), fileName)
                .form("filepath", filePath);
        return request.execute();
    }
    public String uploadFileReturnUrl(String filePath, MultipartFile file, String fileName) throws IOException {
        Objects.requireNonNull(filePath);
        Objects.requireNonNull(file);
        Objects.requireNonNull(fileName);
        String bucketUrl = uploadUrl ;
        HttpRequest request = HttpUtil.createPost(bucketUrl)
                .header("Authorization", "Token " + token)
                .form("filename", file.getBytes(), fileName)
                .form("filepath", filePath);
        if (request.execute().isOk()){
            return downloadUrl + filePath+"/"+fileName;
        }else {
            return null;
        }
    }
    public String uploadFileByByteReturnUrl(String filePath, byte[] bytes, String fileName) throws IOException {
        Objects.requireNonNull(filePath);
        Objects.requireNonNull(bytes);
        Objects.requireNonNull(fileName);
        String bucketUrl = uploadUrl;
        HttpRequest request = HttpUtil.createPost(bucketUrl)
                .header("Authorization", "Token " + token)
                .form("filename", bytes, fileName)
                .form("filepath", filePath);
        HttpResponse execute = request.execute();
        System.out.println(execute.body());
        if (execute.isOk()){
            return downloadUrl + filePath+"/"+fileName;
        }else {
            return null;
        }
    }
}
